package org.mobai.controller;


import cn.hutool.core.util.RandomUtil;
import org.mobai.mapper.OrderDao;
import org.mobai.model.Config;
import org.mobai.model.OrderItem;
import org.mobai.model.User;
import org.mobai.service.IConfigService;
import org.mobai.service.IOrderItemService;
import org.mobai.service.IOrderService;
import org.mobai.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author Administrator
 */
@RequestMapping("user")
@RestController
public class UserController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("testInsert")
    public void test() {
        orderService.testInsertOrder();
    }


    @RequestMapping("/list")
    public void list() {
        orderService.queryById();
    }

    @RequestMapping("/student")
    public void student() {

        orderService.insertStudent();
    }


    @RequestMapping("/person")
    public void person() {
        orderService.insertPerson();
    }

    @Autowired
    private IOrderService iOrderService;

    @RequestMapping("/listPage")
    public void testListPage() {
        iOrderService.listPage();
    }

    @Autowired
    private IConfigService configService;

    @RequestMapping("/testBroadCast")
    public void testBroadCast() {
        Config config = new Config();
        config.setCode("code");
        config.setName("name");
        configService.save(config);

        List<Config> list = configService.list();
        System.out.println(list);

    }

    @Autowired
    private IOrderItemService orderItemService;

    @RequestMapping("/testPrecs")
    public void testPrecs() {

        OrderItem orderItem = new OrderItem();
        orderItem.setItemCode("itemCode" + RandomUtil.randomInt(10));
        orderItem.setItemName("name" + RandomUtil.randomInt(10, 20));
        orderItem.setItemId(RandomUtil.randomInt(10, 30));
        orderItem.setTime(new Date());
        orderItemService.save(orderItem);
        List<OrderItem> list = orderItemService.list();
        System.out.println(list);
    }


    @RequestMapping("/testRange")
    public void testRange() {
        orderItemService.listByParam();
    }


    @RequestMapping("save")
    public String save() {
        User user = new User();
        user.setUserId(new Random().nextInt( 1000 ) + 1);
        user.setName("badao"+user.getUserId());
        user.setAge(new Random().nextInt( 80 ) + 1);
        return "save success";
    }

    @RequestMapping("select")
    public User select() {
//        List<User> all = userService.findAll();
        List<User> all = new ArrayList<>();
        User user = new User();
        user.setUserId(new Random().nextInt( 1000 ) + 1);
        user.setName("badao"+user.getUserId());
        user.setAge(new Random().nextInt( 80 ) + 1);
        user.setIdCard("120222198907102022");

        User user1 = new User();
        user1.setUserId(new Random().nextInt( 1000 ) + 1);
        user1.setName("badao"+user.getUserId());
        user1.setAge(new Random().nextInt( 80 ) + 1);
        all.add(user);
        all.add(user1);
        return all.get(0);
    }
    @Autowired
    OrderDao orderDao;

    @RequestMapping("testInsert")
    public void testInsertOrder() {

        for (int i = 0; i < 10; i++) {
            orderDao.insertOrder(100 + i, "大冰箱" + i);
        }
    }
}
